package com.erp.gather.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.gather.api.domain.ErpSellerProductSku;
import com.erp.gather.domain.bo.ErpSellerProductSkuBo;
import com.erp.gather.domain.vo.ErpSellerProductSkuVo;
import com.erp.gather.mapper.ErpSellerProductSkuMapper;
import com.erp.gather.service.IErpSellerProductSkuService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * ERP个人商品SKU;参考AE-商品：发布产品信息（aliexpress.postproduct.redefining.postaeproduct）Service业务层处理
 *
 * @author erp
 * @date 2023-03-28
 */
@RequiredArgsConstructor
@Service
public class ErpSellerProductSkuServiceImpl implements IErpSellerProductSkuService {

    private final ErpSellerProductSkuMapper baseMapper;

    /**
     * 查询ERP个人商品SKU;参考AE-商品：发布产品信息（aliexpress.postproduct.redefining.postaeproduct）
     */
    @Override
    public ErpSellerProductSkuVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询ERP个人商品SKU;参考AE-商品：发布产品信息（aliexpress.postproduct.redefining.postaeproduct）列表
     */
    @Override
    public TableDataInfo<ErpSellerProductSkuVo> queryPageList(ErpSellerProductSkuBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpSellerProductSku> lqw = buildQueryWrapper(bo);
        Page<ErpSellerProductSkuVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询ERP个人商品SKU;参考AE-商品：发布产品信息（aliexpress.postproduct.redefining.postaeproduct）列表
     */
    @Override
    public List<ErpSellerProductSkuVo> queryList(ErpSellerProductSkuBo bo) {
        LambdaQueryWrapper<ErpSellerProductSku> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpSellerProductSku> buildQueryWrapper(ErpSellerProductSkuBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpSellerProductSku> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSellerProductId() != null, ErpSellerProductSku::getSellerProductId, bo.getSellerProductId());
        lqw.eq(StringUtils.isNotBlank(bo.getSkuCode()), ErpSellerProductSku::getSkuCode, bo.getSkuCode());
        lqw.like(StringUtils.isNotBlank(bo.getSkuName()), ErpSellerProductSku::getSkuName, bo.getSkuName());
        lqw.eq(StringUtils.isNotBlank(bo.getSkuNameEn()), ErpSellerProductSku::getSkuNameEn, bo.getSkuNameEn());
        lqw.eq(StringUtils.isNotBlank(bo.getSkuImage()), ErpSellerProductSku::getSkuImage, bo.getSkuImage());
        lqw.eq(StringUtils.isNotBlank(bo.getImageUrlList()), ErpSellerProductSku::getImageUrlList, bo.getImageUrlList());
        lqw.eq(StringUtils.isNotBlank(bo.getCollectSkuPrice()), ErpSellerProductSku::getCollectSkuPrice, bo.getCollectSkuPrice());
        lqw.eq(bo.getSkuLength() != null, ErpSellerProductSku::getSkuLength, bo.getSkuLength());
        lqw.eq(bo.getSkuWidth() != null, ErpSellerProductSku::getSkuWidth, bo.getSkuWidth());
        lqw.eq(bo.getSkuHeight() != null, ErpSellerProductSku::getSkuHeight, bo.getSkuHeight());
        lqw.eq(bo.getSkuWeight() != null, ErpSellerProductSku::getSkuWeight, bo.getSkuWeight());
        lqw.eq(StringUtils.isNotBlank(bo.getSkuLink()), ErpSellerProductSku::getSkuLink, bo.getSkuLink());
        lqw.eq(StringUtils.isNotBlank(bo.getSkuPrice()), ErpSellerProductSku::getSkuPrice, bo.getSkuPrice());
        lqw.eq(bo.getSkuStock() != null, ErpSellerProductSku::getSkuStock, bo.getSkuStock());
        lqw.eq(StringUtils.isNotBlank(bo.getSkuPropertyIds()), ErpSellerProductSku::getSkuPropertyIds, bo.getSkuPropertyIds());
        lqw.eq(StringUtils.isNotBlank(bo.getRevision()), ErpSellerProductSku::getRevision, bo.getRevision());
        return lqw;
    }

    /**
     * 新增ERP个人商品SKU;参考AE-商品：发布产品信息（aliexpress.postproduct.redefining.postaeproduct）
     */
    @Override
    public Boolean insertByBo(ErpSellerProductSkuBo bo) {
        ErpSellerProductSku add = BeanUtil.toBean(bo, ErpSellerProductSku.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改ERP个人商品SKU;参考AE-商品：发布产品信息（aliexpress.postproduct.redefining.postaeproduct）
     */
    @Override
    public Boolean updateByBo(ErpSellerProductSkuBo bo) {
        ErpSellerProductSku update = BeanUtil.toBean(bo, ErpSellerProductSku.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpSellerProductSku entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除ERP个人商品SKU;参考AE-商品：发布产品信息（aliexpress.postproduct.redefining.postaeproduct）
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
